<template>
  <div class="app-container">
    <el-card>
      <!--工具栏-->
      <div class="head-container">
        <eHeader :permission="permission"/>
        <crudOperation :permission="permission" />
      </div>
      <!--工具栏-->

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="quantity" label="招聘人数" />
        <el-table-column prop="city" label="工作地点" />
        <el-table-column prop="salaryUp" label="最高薪资" />
        <el-table-column prop="salaryDown" label="最低薪资" />
        <el-table-column prop="status" label="状态" >
          <template slot-scope="scope">
            <el-button v-if="scope.row.status == 1" type="warning" size="mini">审核中</el-button>
            <el-button v-else-if="scope.row.status == 2" type="success" size="mini">通过啦</el-button>
            <el-button v-else-if="scope.row.status == 3"type="danger" size="mini">拒绝啦</el-button>
            <el-button v-else type="primary" size="mini">创建</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="companyName" label="所属公司" />
        <el-table-column prop="cateName" label="类型" />
        <el-table-column prop="hrName" label="hr" />

        <el-table-column  label="操作" width="150px" align="center">
            <!--<template slot-scope="scope">
              <udOperation
                :data="scope.row"
                :permission="permission"
              />
            </template>-->
          <template slot-scope="scope">
            <el-button v-if="scope.row.status === 1" @click="passEvent(scope.row)" type="success" size="mini">通过</el-button>
            <el-button v-if="scope.row.status === 1 && scope.row.status !== 3" @click="rejectEvent(scope.row)" type="warning" size="mini">拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </el-card>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible="crud.status.cu > 0"
      :title="crud.status.title"
      width="500px"
    >
      <el-form ref="form" :model="form" size="small" label-width="80px">
        <el-form-item label="名称" prop="nam">
          <el-input v-model="form.name" style="width: 370px;"/>
        </el-form-item>
        <el-form-item label="类型" prop="categoryId">
          <el-select v-model="form.categoryId" style="width: 178px" placeholder="请先选择类型">
            <el-option
              v-for="(item, index) in categoryList"
              :key="item.name + index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="招聘人数" prop="quantity">
          <el-input v-model="form.quantity" style="width: 370px;"/>
        </el-form-item>
        <el-form-item label="工作地点" prop="city">
          <el-input v-model="form.city" style="width: 370px;"/>
        </el-form-item>
        <el-form-item label="最低薪资" prop="salaryDown">
          <el-input v-model="form.salaryDown" style="width: 370px;"/>
        </el-form-item>
        <el-form-item label="最高薪资" prop="salaryUp">
          <el-input v-model="form.salaryUp" style="width: 370px;"/>
        </el-form-item>
        <el-form-item label="职位要求" prop="requirement">
          <el-input v-model="form.requirement" style="width: 370px;"/>
        </el-form-item>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="text"
          @click="crud.cancelCU"
        >
          取消
        </el-button>
        <el-button
          :loading="crud.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >
          确认
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import crudJob, {audit} from '@/api/ceamJob'
import {getCategoryList} from '@/api/ceamCategory'

import eHeader from './header'
import CRUD, { presenter, form } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

// crud交由presenter持有
const crud = CRUD({
  title: '职位管理',
  url: 'api/companyJob',
  sort: ['sort,asc', 'id,desc'],
  crudMethod: { ...crudJob }
})
const defaultForm = { name: null, img: '' }
export default {
  components: { eHeader, crudOperation, pagination, udOperation },
  mixins: [presenter(crud),form(defaultForm)],
  data() {
    return {
      categoryList: [],
      headers: {
        'Authorization': getToken()
      },
      delLoading: false,
      permission: {
        add: ['admin', 'hrJo:add'],
        edit: ['admin', 'hrJo:edit'],
        del: ['admin', 'hrJo:del']
      }
    }
  },
  computed: {
    ...mapGetters([
      'uploadImgApi',
      'baseApi'
    ])
  },
  created() {
    this.$nextTick(() => {
      //this.init()
    })
  },
  methods: {
    checkPermission,
    [CRUD.HOOK.beforeToCU]() {
      getCategoryList({page: 0, size: 500}).then(res => {
        this.categoryList = res.records;
      })
    },
    // 提交前的验证
    [CRUD.HOOK.afterValidateCU]() {

      return true
    },
    passEvent(data) {
      data.status = 2;
      audit(data).then(res => {
        this.$message({
          message: '成功',
          type: 'success'
        });

      }).catch(error => {
        this.$message({
          message: '失败',
          type: 'warning'
        });
      })
    },
    rejectEvent(data) {
      data.status = 3;
      audit(data).then(res => {
        this.$message({
          message: '成功',
          type: 'success'
        });
        //this.init()
      }).catch(error => {
        this.$message({
          message: '失败',
          type: 'warning'
        });
      })
    }
  }
}
</script>

<style scoped>

</style>
